function [movement,velxy] = calculate_onset_offset( raw_data,fs,mid )

% load data
% calculate velocity
% calculate onset & offset
[b,a] = butter(3,5/(fs/2),'low');
filt_hand_x = filtfilt(b,a,raw_data.hand_x);
filt_hand_y = filtfilt(b,a,raw_data.hand_y);

diffx = diff(filt_hand_x);
diffy = diff(filt_hand_y);
velxy = sqrt((diffx.^2+diffy.^2))*fs;

% [max_v,position] = max(velxy);
% onset_velxy = velxy(position-mid:length(velxy));
% %onset
% IniNum = find(onset_velxy > 0.1 * max_v);
% movement.onset = IniNum(1) + position-mid -1;
% %offset
% offset_velxy = velxy(position:length(velxy));
% LastNum = find(offset_velxy < 0.1 * max_v);
% movement.offset = LastNum(1) + position -1;

[max_v,position] = max(velxy);
onset_velxy = velxy(1:position);
%onset
IniNum = find(onset_velxy < 0.10 * max_v);
movement.onset = IniNum(end);
%offset
offset_velxy = velxy(position:length(velxy));
LastNum = find(offset_velxy < 0.10 * max_v);
movement.offset = LastNum(1) + position -1;
end

